#!user/bin/env python3
# -- coding: utf-8 --
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service
import requests
import time
from lxml import etree

driver = webdriver.Edge(service=Service(r'/MicrosoftWebDriver.exe'))
driver.get('https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx')
email = driver.find_element(By.CSS_SELECTOR, '#email')
email.clear()
email.send_keys('1478243841@qq.com')
pwd = driver.find_element(By.CSS_SELECTOR, '#pwd')
pwd.clear()
pwd.send_keys('123456')
code = input('请输入验证码：')
pwd = driver.find_element(By.CSS_SELECTOR, '#code')
pwd.send_keys(code)
driver.find_element(By.CSS_SELECTOR, '#denglu').click()
time.sleep(3)
# source=driver.page_source
cookies = driver.get_cookies()
cookie = {}
for i in cookies:
    cookie[i['name']] = i['value']
url = driver.current_url
# print(cookie)
user_agent = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41 Safari/537.36 Edg/101.0.1210.32'
}
r = requests.get(url=url, headers=user_agent, cookies=cookie).text
anlytical = etree.HTML(r)
title = anlytical.xpath('//div[@class="sons"]//a//text()')
href = anlytical.xpath('//div[@class="sons"]//a//@href')
title = [''.join(title[i:i+2]) for i in range(0, len(title), 2)]
href = ['https://so.gushiwen.cn' + href for href in href]
dicts = dict(zip(title, href))
for title, href in dicts.items():
    print(title, href)
